include( 'shared.lua' )
include( 'cl_hud.lua' )

inOverview = false

local pushEmitter = nil
local pushEmitterPositions = {}
for i=1, 4 do pushEmitterPositions[i] = {} end
local pushEmitterEnabled = {}
for i=1, 4 do pushEmitterEnabled[i] = {} end
local pushLastThink = 0
local pushThink = 0.2

function GM:Initialize()

	gui.EnableScreenClicker( true )

	pushEmitter = ParticleEmitter( Vector( 0, 0, 0 ) )
	
	for teamID=1, 4 do
		for checkpoint=1, 3 do
			pushEmitterPositions[teamID][checkpoint] = Vector( 0, 0, 0 )
			pushEmitterEnabled[teamID][checkpoint] = false
		end
	end
	
end

function UpdatePushEmitter( um )

	local id = um:ReadShort()
	local checkpoint = um:ReadShort()
	local enabled = um:ReadBool()
	local pos = um:ReadVector()

	pushEmitterEnabled[id][checkpoint] = enabled
	pushEmitterPositions[id][checkpoint] = pos

end
usermessage.Hook( "UpdatePushEmitter", UpdatePushEmitter )

function PushParticles()

	if ( pushLastThink < CurTime() ) then
		
		for id=1, 4 do
			for checkpoint=1,3 do
				if ( pushEmitterEnabled[id][checkpoint] ) then
					local size = math.random(6, 12)
					local particle = pushEmitter:Add("effects/bubble", pushEmitterPositions[id][checkpoint] + Vector( math.random(-20,20), math.random(-20,20), 0))
					particle:SetColor(255,255,255,255)
					particle:SetGravity(Vector(0,0,100))
					particle:SetDieTime(2.2)
					particle:SetLifeTime(0)
					particle:SetStartSize(size)
					particle:SetEndSize(size)
					particle:SetStartAlpha(255)
					particle:SetEndAlpha(0)
				end
			end
		end
		
		pushLastThink = CurTime() + pushThink
		
	end

end
hook.Add( "Think", "TriggerPushParticles", PushParticles )

function CPlayerInitialSpawn( um )

	for k, ply in pairs( player.GetAll() ) do
	
		if ( ply and not ply:IsValid() ) then return end

		function ply.BuildBonePositions()  
			local boneIndex = ply:LookupBone( "ValveBiped.Bip01_Head1" )  
			local boneMatrix = ply:GetBoneMatrix( boneIndex )  
			boneMatrix:Scale( Vector(2.8, 2.8, 2.8) )  
			ply:SetBoneMatrix( boneIndex, boneMatrix )  
			
			boneIndex = ply:LookupBone( "ValveBiped.Bip01_L_Forearm" )  
			boneMatrix = ply:GetBoneMatrix( boneIndex )  
			boneMatrix:Scale( Vector(1, 2, 2) )  
			ply:SetBoneMatrix( boneIndex, boneMatrix )  
			
			boneIndex = ply:LookupBone( "ValveBiped.Bip01_R_Forearm" )  
			boneMatrix = ply:GetBoneMatrix( boneIndex )  
			boneMatrix:Scale( Vector(1, 2, 2) )  
			ply:SetBoneMatrix( boneIndex, boneMatrix )  
			
			boneIndex = ply:LookupBone( "ValveBiped.Bip01_L_Calf" )  
			boneMatrix = ply:GetBoneMatrix( boneIndex )  
			boneMatrix:Scale( Vector(1, 2, 2) )  
			ply:SetBoneMatrix( boneIndex, boneMatrix )  
			
			boneIndex = ply:LookupBone( "ValveBiped.Bip01_R_Calf" )  
			boneMatrix = ply:GetBoneMatrix( boneIndex )  
			boneMatrix:Scale( Vector(1, 2, 2) )  
			ply:SetBoneMatrix( boneIndex, boneMatrix )  
		end
	
	end
	
end
usermessage.Hook( "CPlayerInitialSpawn_umsg", CPlayerInitialSpawn )



function GM:GUIMousePressed( mc )

	if ( mc == MOUSE_LEFT ) then
		RunConsoleCommand( "+attack", "" )
	end
	
end

function GM:GUIMouseReleased( mc )

	if ( mc == MOUSE_LEFT ) then
		RunConsoleCommand( "-attack", "" )
	end

end

function GM:CreateMove( cmd )

	if ( not LocalPlayer():Alive() ) then return end

	local plyPos = ( LocalPlayer():GetPos() + Vector(0, 0, 64) ):ToScreen()
	local plyvec = Vector( plyPos.x, plyPos.y, 0 )
	
	local pos = Vector( gui.MouseX(), gui.MouseY(), 0 )
	local angle = ( pos-plyvec ):Angle()
	local dirang = 0
	
	if ( pos.x > plyPos.x ) then
		dirang = 0
		angle.y = math.AngleDifference( angle.y, 360 ) // This is needed to fix a nasty Out-of-range warning. (Works fine with or without this line though.)
		cmd:SetForwardMove( cmd:GetSideMove() )
	else
		angle:RotateAroundAxis( Vector(0, 0, 1), 180 )
		angle.y = -angle.y
		dirang = 180	
		cmd:SetForwardMove( -cmd:GetSideMove() )
	end
	
	cmd:SetViewAngles( Angle(angle.y, dirang, 0) )
	cmd:SetSideMove( 0 )
	
end

function GM:PositionScoreboard( ScoreBoard )

	ScoreBoard:SetSize( 700, ScrH() - 100 )
	ScoreBoard:SetPos( (ScrW() - ScoreBoard:GetWide()) / 2, 50 )

end


function BlockedCommands( ply, bind, pressed )

	if ( bind == "+duck" ) then
		if ( ply:OnGround() ) then
			return false
		else
			return true
		end
	end
	
	if ( bind == "+jump" ) then
		if ( ply:KeyDown( IN_DUCK ) ) then
			return true
		else
			return false
		end
	end
	
	local blocked = {}
	
	for k, cmd in pairs( blocked ) do
		if ( string.find( bind, cmd ) ) then
			return true
		end
	end
	
end
hook.Add( "PlayerBindPress", "BlockCommands", BlockedCommands )
 
function GM:CalcView( ply, origin, angles, fov )

	local view = {}

	local numPly = 0
	for k, v in pairs( player.GetAll() ) do
		numPly = numPly + 1
	end
	
	if ( numPly <= 1 and ply:Team() == TEAM_UNASSIGNED or ply:Team() == TEAM_SPECTATOR or !ply:Alive() or inOverview ) then
		view.origin = Vector( 0, -1700, 600 )
		view.angles = Angle( 0, 90, 0 )
		view.fov = 65
	else 
		view.origin = origin + Vector( 0, -500, 0 )
		view.angles = Angle( 0, 90, 0 )
	end
	
	return view

end

function ChangeOverview( um )

	inOverview = um:ReadBool()

end
usermessage.Hook( "ChangeOverview", ChangeOverview )

function GM:ShouldDrawLocalPlayer()

	return true

end

function HideHudElements( name )

	local stringList = {
		"CHudHealth", "CHudBattery", "CHudSuitPower",
		"CHudAmmo", "CHudSecondaryAmmo", "CHudCrosshair",
		"CHudDamageIndicator"
	}
	
	for k, str in pairs( stringList ) do
		if ( str == name ) then
			return false
		end
	end

end
hook.Add( "HUDShouldDraw", "HideHudElements", HideHudElements )
 

